﻿using UnityEngine;
using FairyGUI;
using System;
namespace Spate
{
    public class BaseWindow : MonoBehaviour
    {
        public GComponent _MainVeiw;

        public string _PackageName { get; private set; }
        public string _ViewName { get; private set; }

        public bool _IsOpen { get; private set; }
        public bool _IsFirstOpen { get; private set; }

        public object[] _Args { get; private set; }

        private UIPackage _MainPackage { set; get; }

        public bool IsActive
        {
            get { return _MainVeiw.visible; }
            private set { _MainVeiw.visible = value; }
        }

        public void SetActive(bool isActive)
        {
            IsActive = isActive;
        }

        private const string _UIPath = "UI/";

        public UIPackage AddPackage(string airPath)
        {
            return UIPackage.AddPackage(airPath);
        }

        public UIPackage GetPackageById(string uiId)
        {
            return UIPackage.GetById(uiId);
        }

        public UIPackage GetPackageByName(string packageName)
        {
            return UIPackage.GetByName(packageName);
        }

        public void Init(params object[] args)
        {
            //初始化包
            _Args = args;
            //
            InitMainPackage();
            InitMainView();
            OnCreate(args);
        }

        private void InitMainPackage()
        {
            if (!string.IsNullOrEmpty(_PackageName))
            {
                _MainPackage = GetPackageByName(_PackageName);
                if (_MainPackage == null)
                    AddPackage(_UIPath + _PackageName);
            }
            else
            {
                Debug.LogError(string.Format("窗口{0}没有给定包名", this.GetType().Name));
            }
        }

        private void InitMainView()
        {
            if (_MainPackage != null)
            {
                _MainVeiw = UIPackage.CreateObject(_PackageName, _ViewName).asCom;
                if (_MainVeiw == null)
                    Debug.LogError(string.Format("视图名{0}不正确，无法完成加载", _ViewName));
            }
            else
            {
                Debug.LogError(string.Format("包名{0}不正确，无法完成加载", _PackageName));
            }
        }

        protected virtual void OnCreate(params object[] args)
        {

        }

        protected virtual void OnOpen(bool isReOpen, params object[] args)
        {

        }

        public void Open(bool isPlayforOpen, params object[] args)
        {
            _Args = args;
            OnOpen(_IsOpen, _Args);
            _IsFirstOpen = false;
            _IsOpen = true;
            IsActive = true;
            PlayOpenAnim(isPlayforOpen);
        }

        private void PlayOpenAnim(bool isPlayforOpen)
        {
            if (isPlayforOpen)
            {
                OnPlayAnimBefroeOpen();
                PlayWindowAnim(true, OnPlayAnimAfferOpen);
            }
            else
                OnPlayAnimAfferOpen();
        }

        private void PlayWindowAnim(bool isOpen, Action callback)
        {
            Transition tansition = null;
            if (isOpen)
            {
                //规定打开界面的动画名字叫OpenAnim
                tansition = _MainVeiw.GetTransition("OpenAnim");
            }
            else
            {
                //规定关闭界面的动效名字叫CloseAnim
                tansition = _MainVeiw.GetTransition("CloseAnim");
            }
            if (tansition == null)
                callback();
            else
                tansition.Play(new PlayCompleteCallback(callback));
        }

        /// <summary>
        /// 播放动画后
        /// </summary>
        protected virtual void OnPlayAnimAfferOpen()
        {

        }

        /// <summary>
        /// 播放动画之前
        /// </summary>
        protected virtual void OnPlayAnimBefroeOpen()
        {

        }

        private void PlayCloseAnim(bool destroy)
        {
            Action act = () =>
            {
                _IsOpen = false;
                //用Window管理类关闭自己 
                OnPlayAnimAffterClose();
            };
            PlayWindowAnim(false, act);
        }

        public void DoClose(bool destroy = false)
        {
            OnClose();
            OnPlayAnimBeforeClose();
            PlayCloseAnim(destroy);
            //
        }

        public void Destroy()
        {
            _MainVeiw.RemoveFromParent();
            //销毁之后的一些处理
        }

        protected virtual void OnPlayAnimAffterClose()
        {

        }

        protected virtual void OnPlayAnimBeforeClose()
        {

        }

        protected virtual void OnClose()
        {

        }

        protected virtual void OnUpdate()
        {

        }

        protected virtual void OnDestroy()
        {

        }

        void Update()
        {
            OnUpdate();
        }
    }
}
